
/*
 * Copyright (C) 2008-2009 Archie L. Cobbs. All rights reserved.
 *
 * $Id: InvalidAnnotationException.java 90 2009-04-23 03:07:07Z archie.cobbs $
 */

package org.dellroad.sidekar.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * Exception indicating an invalid Sidekar schema annotation.
 *
 * @see org.dellroad.sidekar.annotation
 */
@SuppressWarnings("serial")
public class InvalidAnnotationException extends IllegalArgumentException {

    private final Annotation a;
    private final Class<?> annotatedClass;
    private final Method annotatedMethod;

    InvalidAnnotationException(Annotation a, Class<?> cl, String message) {
        super("invalid annotation " + desc(a) + " on " + cl + (message != null ? ": " + message : ""));
        this.a = a;
        this.annotatedClass = cl;
        this.annotatedMethod = null;
    }

    InvalidAnnotationException(Annotation a, Class<?> cl) {
        this(a, cl, null);
    }

    InvalidAnnotationException(Annotation a, Method method, String message) {
        super("invalid annotation " + desc(a) + " on method " + method + (message != null ? ": " + message : ""));
        this.a = a;
        this.annotatedClass = null;
        this.annotatedMethod = method;
    }

    InvalidAnnotationException(Annotation a, Method method) {
        this(a, method, null);
    }

    /**
     * Get the annotation that was deemed invalid.
     */
    public Annotation getAnnotation() {
        return this.a;
    }

    /**
     * Get the class to which the annotation was attached (if any).
     */
    public Class<?> getAnnotatedClass() {
        return this.annotatedClass;
    }

    /**
     * Get the method to which the annotation was attached (if any).
     */
    public Method getAnnotatedMethod() {
        return this.annotatedMethod;
    }

    private static String desc(Annotation a) {
        return String.valueOf(a).replace(InvalidAnnotationException.class.getPackage().getName() + ".", "");
    }
}

